﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BasicCMS.Content.Parts;
using BasicCMS.Data;

namespace BasicCMS.Content.Controllers
{
    public class ContentController : Controller
    {
        public ActionResult Edit(int? id)
        {
            var contentType = ModelHelper.CreateContentType();
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                var row= unitOfWork.Table(contentType.TableName)
                    .Where("Id=@Id")
                    .Param("Id", id)
                    .ToRow();

                ModelHelper.BuildViewState(contentType, row, this);
            }
            return View(contentType);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection form,int? id)
        {
            var contentType = ModelHelper.CreateContentType();
            ModelHelper.BuildViewState(contentType, form, this);
            IDictionary<string, object> data = ModelHelper.GetContentValue(contentType);
            if (data.Count > 0)
            {
                using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                {
                    if (id == null)
                    {
                        var builder = unitOfWork.Insert(contentType.TableName);
                        foreach (var item in data)
                        {
                            builder.Value(item.Key, item.Value);
                        }
                    }
                    else
                    {
                        var builder = unitOfWork.Update(contentType.TableName);
                        foreach (var item in data)
                        {
                            builder.Set(item.Key, item.Value);
                        }
                        builder.Where("Id=@Id").Param("Id", id.Value);
                    }
                    unitOfWork.Commit();
                }
                ViewData["Success"] = true;
            }

            return View(contentType);
        }

        public ActionResult List(FormCollection form, int? pageIndex)
        {
            pageIndex = pageIndex ?? 1;
            PagedTable result = null;
            var contentType = ModelHelper.CreateContentType();

            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                result = unitOfWork.Table(contentType.TableName).OrderBy("Id").ToPagedTable(pageIndex.Value, 10);
            }
            return View(result);
        }

        public ActionResult View(int id)
        {
            var contentType = ModelHelper.CreateContentType();

            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                var row = unitOfWork.Table(contentType.TableName)
                    .Where("Id=@Id")
                    .Param("Id", id)
                    .ToRow();

                return View(row);
            }
        }

        public ActionResult Delete(int[] id)
        {
            var contentType = ModelHelper.CreateContentType();

            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                foreach (var item in id)
                {
                    unitOfWork.Delete(contentType.TableName).Where("Id=@Id").Param("Id", item);
                }
                unitOfWork.Commit();
            }
            return RedirectToAction("List");
        }

    }
}
